//
//  IWxChatMgrGroup.m
//  WXIM
//
//  Created by fusuwei on 14-10-21.
//  Copyright (c) 2014年 naren. All rights reserved.
//

#import "IWxChatMgrGroup.h"
#import "UcfgroupXMPPManger.h"
#import "PublicMacros.h"
#import "EPublicMacros.h"
#import "ChatLoginUser.h"
#import "ChatLoginUserMgr.h"
#import "ChatContact.h"
#import "ChatMessageMgr.h"
#import "Utility.h"
#import "ContactMemModel.h"


@implementation IWxChatMgrGroup
{
    
}


static IWxChatMgrGroup *sharedGroup;



+(IWxChatMgrGroup*)sharedGroup{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedGroup =[[IWxChatMgrGroup alloc] init];
       
    });
    return sharedGroup;
}



#pragma mark - Group action

/*!
 @method
 @brief  初始化并获取自己的所有群组信息
 @return 初始化执行成功or失败
 */

//根据当前用户获取所有群及群成员
//根据版本号ver获取群及群成员
//需要注意的地方：返回的结果是group和group_user
//在groups和group中分别又个replace属性。当group中replace=1时表示此用户删除过群组或被T出了群组。客户端需要更新本地group信息。 当groups中的replace=1是表示此用户所在的组中的成员数量发生变化。 replace=0是只表示用户属性或群属性发生变化
- (BOOL)initAllContactGroups
{
    //---vince@2014-10-22
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *groupVer = [defaults objectForKey:@"GroupVer"];
    if (groupVer == nil || [groupVer isEqualToString:@""]) {
        groupVer = @"0";
    }
    
    UcfgroupXMPPManger* delelgate = [UcfgroupXMPPManger sharedInstance];
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"get"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"initAllContactGroups"];
    
    NSXMLElement *get_group = [NSXMLElement elementWithName:@"get_group"];
    [get_group addAttributeWithName:@"ver" stringValue:groupVer];
        
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:get_group];
    
    [iq addChild:group];
    
    [delelgate.xmppStream sendElement:iq];
    
    [delelgate addXmppRecordReqId:@"initAllContactGroups" type:@"initAllContactGroups" paraMsg:nil];
    
    return YES;
}

- (void)initAllContactGroups:(void (^)(BOOL state))completion
{
    //---vince@2014-10-22
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *groupVer = [defaults objectForKey:@"GroupVer"];
    if (groupVer == nil || [groupVer isEqualToString:@""]) {
        groupVer = @"0";
    }
    
    UcfgroupXMPPManger* delelgate = [UcfgroupXMPPManger sharedInstance];
    delelgate.contactGroupCb = completion;
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"get"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"initAllContactGroups"];
    
    NSXMLElement *get_group = [NSXMLElement elementWithName:@"get_group"];
    [get_group addAttributeWithName:@"ver" stringValue:groupVer];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:get_group];
    
    [iq addChild:group];
    
    [delelgate.xmppStream sendElement:iq];
    
    [delelgate addXmppRecordReqId:@"initAllContactGroups" type:@"initAllContactGroups" paraMsg:nil];
}

/*!
 @method
 @brief  获取当前用户的所有群组列表
 @return 返回ChatGroup对象数组
 */
- (NSMutableArray *)getContactGroups
{
    NSMutableArray *groupList = [ChatGroupMgr readAllGroupListArray];
    return groupList;
}

/*!
 @method
 @brief  creatGroup
 @param  groupName      群组名称
 @param  groupDesc      群组描述信息
 @param  maxNum         群成员最大数限制
 @param  yesorno        是否是公开群　０不公开，１公开
 @param  approval       是否需要审批加入，此属性只针对公开群组有效
 @return 返回这个方法     creatGroupNameRspState: groupId: withMsg:
 */
- (void)creatGroupName:(NSString *)groupName
             groupDesc:(NSString *)groupDesc
             memberMax:(NSInteger)maxNum
              isPublic:(BOOL)yesorno
            isApproval:(BOOL)isapproval
              delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(creatGroupNameRspState:groupId:withMsg:)])
    {
        if (![delegateUcf isLogSuccess])
        {
            NSString *msg = [NSString stringWithFormat:@"%@ 没有登录！",groupName];
            [delegateUcf.xmppGroupDelegate creatGroupNameRspState:Group_NotLogin groupId:nil withMsg:msg];
            return;
        }
        if (groupName == nil || [groupName isEqualToString:@""])
        {
            NSString *msg = [NSString stringWithFormat:@"%@ 不能为空！",groupName];
            [delegateUcf.xmppGroupDelegate creatGroupNameRspState:Group_GroupNameIsNull groupId:nil withMsg:msg];
            return;
        }
        if (groupDesc == nil || [groupDesc isEqualToString:@""])
        {
            NSString *msg = [NSString stringWithFormat:@"%@ 不能为空！",groupName];
            [delegateUcf.xmppGroupDelegate creatGroupNameRspState:Group_GroupDescIsNull groupId:nil withMsg:msg];
            return;
        }
        if ([groupDesc length] > kMessageBodyMaxLen)
        {
            NSString *msg = [NSString stringWithFormat:@"%@ 内容超长！",groupName];
            [delegateUcf.xmppGroupDelegate creatGroupNameRspState:Group_GroupDescIsNull groupId:nil withMsg:msg];
            return;
        }
        if (maxNum > kGroupMemberMaxNum)
        {
            NSString *msg = [NSString stringWithFormat:@"%@ 成员数最大不能超过%d！",groupName,kGroupMemberMaxNum];
            [delegateUcf.xmppGroupDelegate creatGroupNameRspState:Group_GroupMemberMaxError groupId:nil withMsg:msg];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                   message:@"请添加EWxChatMgrGroupDelegate协议并实现creatGroupNameRspState: groupId: withMsg:方法！"
                                  delegate:nil
                         cancelButtonTitle:@"确定"
                         otherButtonTitles:nil] show];
        return;
    }
    
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_CreateGroupName:groupName
                                                             desc:groupDesc
                                                        memberMax:maxNum
                                                         isPublic:yesorno
                                                       isApproval:isapproval];
    return;
}

//group_name:群组名称 desc:群描述信息 public:是否是公开群　０不公开，１公开 approval:是否需要审批加入，此属性只针对公开群组有效。
/*!
 @method
 @brief        异步方法,    更新群组
 @param        groupId     群组ID
 @param        groupName   群组新名称
 @param        maxNum      群成员最大数限制
 @discussion   updateGroupIdRspState:chatGroup:error:
 */
- (void)updateGroupId:(NSString *)groupId groupName:(NSString *)groupName memberMax:(NSInteger)maxNum delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(updateGroupIdRspState:chatGroup:error:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupIdNotExist chatGroup:theChatGroup error:[NSString stringWithFormat:@"%@ 不存在！",groupId]];
            return;
        }
    
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_NotLogin chatGroup:theChatGroup error:[NSString stringWithFormat:@"%@ 没有登录！",groupId]];
            return;
        }
        if (groupName == nil || [groupName isEqualToString:@""])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupNameIsNull chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组名称为空！"]];
            return;
        }
        if ([groupName length] > kGroupNameMaxNum)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupNameMaxLenError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组名称超长！"]];
            return;
        }
        if (maxNum > kGroupMemberMaxNum)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupMemberMaxError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组成员数超过最大值！"]];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现updateGroupIdRspState:chatGroup:error:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"updateGroupId:groupDesc:ispublic"];
    
    NSXMLElement *create = [NSXMLElement elementWithName:@"create"];
    [create addAttributeWithName:@"group_id" stringValue:groupId];
    [create addAttributeWithName:@"group_name" stringValue:groupName];
    [create addAttributeWithName:@"desc" stringValue:theChatGroup.group_desc];
    [create addAttributeWithName:@"public" stringValue:theChatGroup.group_public];
    [create addAttributeWithName:@"max" stringValue:[NSString stringWithFormat:@"%ld",(long)maxNum]];
    [create addAttributeWithName:@"approval" stringValue:theChatGroup.group_approval];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:create];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"updateGroupId:groupDesc:ispublic" type:@"updateGroupId" paraMsg:theChatGroup];
    return;
}

/*!
 @method
 @brief        异步方法,    更新群组（除名称外所有）属性
 @param        groupId     群组ID
 @param        groupDesc   群组描述信息
 @param        maxNum      群成员最大数限制
 @param        public      是否是公开群 　０不公开，１公开
 @param        approval    是否需要审批加入，此属性只针对公开群组有效
 @discussion   updateGroupIdRspState:chatGroup:error:
 */
- (void)updateGroupId:(NSString *)groupId groupDesc:(NSString *)groupDesc memberMax:(NSInteger)maxNum ispublic:(BOOL)ispublic isNeedapproval:(BOOL)isNeed delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(updateGroupIdRspState:chatGroup:error:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupIdNotExist chatGroup:theChatGroup error:[NSString stringWithFormat:@"%@ 不存在！",groupId]];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_NotLogin chatGroup:theChatGroup error:[NSString stringWithFormat:@"%@ 没有登录！",groupId]];
            return;
        }
        if (groupDesc == nil || [groupDesc isEqualToString:@""])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupDescIsNull chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组描述为空！"]];
            return;
        }
        
        if ([groupDesc length] > kGroupDescribeMaxLen)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupDescMaxLenError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组描述超长！"]];
            return;
        }
        if (maxNum > kGroupMemberMaxNum)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupMemberMaxError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组成员数超过最大值！"]];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现updateGroupIdRspState:chatGroup:error:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"updateGroupId:groupDesc:ispublic"];
    
    NSXMLElement *create = [NSXMLElement elementWithName:@"create"];
    [create addAttributeWithName:@"group_id" stringValue:groupId];
    [create addAttributeWithName:@"group_name" stringValue:theChatGroup.group_name];
    [create addAttributeWithName:@"desc" stringValue:groupDesc];
    [create addAttributeWithName:@"public" stringValue:[NSString stringWithFormat:@"%d",ispublic]];
    [create addAttributeWithName:@"max" stringValue:[NSString stringWithFormat:@"%ld",(long)maxNum]];
    [create addAttributeWithName:@"approval" stringValue:[NSString stringWithFormat:@"%d",isNeed]];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:create];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"updateGroupId:groupDesc:ispublic" type:@"updateGroupId" paraMsg:theChatGroup];
    return ;
}

/*!
 @method
 @brief        异步方法,   更新群组所有属性
 @param        groupId    群组ID
 @param        groupName  群组新名称
 @param        groupDesc  群组描述信息
 @param        maxNum     群成员最大数限制
 @param        public     是否是公开群 　０不公开，１公开
 @param        approval   是否需要审批加入，此属性只针对公开群组有效
 @discussion   updateGroupIdRspState:chatGroup:error:
 */
- (void)updateGroupId:(NSString *)groupId groupName:(NSString *)groupName groupDesc:(NSString *)groupDesc memberMax:(NSInteger)maxNum ispublic:(BOOL)ispublic isNeedapproval:(BOOL)isNeed delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(updateGroupIdRspState:chatGroup:error:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupIdNotExist chatGroup:theChatGroup error:[NSString stringWithFormat:@"%@ 不存在！",groupId]];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_NotLogin chatGroup:theChatGroup error:[NSString stringWithFormat:@"%@ 没有登录！",groupId]];
            return;
        }
        
        if (groupName == nil || [groupName isEqualToString:@""])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupNameIsNull chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组名称为空！"]];
            return;
        }
        if ([groupName length] > kGroupNameMaxNum)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupNameMaxLenError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组名称超长！"]];
            return;
        }
        
        if (groupDesc == nil || [groupDesc isEqualToString:@""])
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupDescIsNull chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组描述为空！"]];
            return;
        }
        if ([groupDesc length] > kGroupDescribeMaxLen)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupDescMaxLenError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组描述超长！"]];
            return;
        }
        if (maxNum > kGroupMemberMaxNum)
        {
            [delegateUcf.xmppGroupDelegate updateGroupIdRspState:Group_GroupMemberMaxError chatGroup:theChatGroup error:[NSString stringWithFormat:@"群组成员数超过最大值！"]];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现updateGroupIdRspState:chatGroup:error:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"updateGroupId:groupDesc:ispublic"];
    
    NSXMLElement *create = [NSXMLElement elementWithName:@"create"];
    [create addAttributeWithName:@"group_id" stringValue:groupId];
    [create addAttributeWithName:@"group_name" stringValue:groupName];
    [create addAttributeWithName:@"desc" stringValue:groupDesc];
    [create addAttributeWithName:@"max" stringValue:[NSString stringWithFormat:@"%ld",(long)maxNum]];
    [create addAttributeWithName:@"public" stringValue:[NSString stringWithFormat:@"%d",ispublic]];
    [create addAttributeWithName:@"approval" stringValue:[NSString stringWithFormat:@"%d",isNeed]];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:create];
    
    [iq addChild:group];
    
    UcfgroupXMPPManger* delelgate = [UcfgroupXMPPManger sharedInstance];
    [delelgate.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"updateGroupId:groupDesc:ispublic" type:@"updateGroupId" paraMsg:theChatGroup];
    return ;
}


/*!
 @method
 @brief       同步方法, 获取群组对象信息
 @param       groupId  群组ID
 @discussion  返回ChatGroup对象，若groupId不存在则返回nil
 */
- (ChatGroup *)getGroupInfoByGroupId:(NSString *)groupId;
{
    if (groupId == nil) {
        return nil;
    }
    ChatGroup *groupItem = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    return groupItem;
}

/*!
 @method
 @brief      异步方法, 删除某个群组
 @param      groupId 要退出的群组Id
 @discussion
 @result     回调方法deleteGroupIdRspState:groupId:
 */
- (void)deleteGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(deleteGroupIdRspState:groupId:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate deleteGroupIdRspState:Group_GroupIdNotExist groupId:groupId];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate deleteGroupIdRspState:Group_NotLogin groupId:groupId];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现deleteGroupIdRspState:groupId:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }

    if(![self isTheOwnerOfGroupId:groupId])
    {
        [self quitGroupId:groupId delegate:delegate];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"deleteGroupId:"];
    
    NSXMLElement *delete = [NSXMLElement elementWithName:@"delete"];
    [delete addAttributeWithName:@"group_id" stringValue:groupId];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:delete];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"deleteGroupId:" type:@"deleteGroupId" paraMsg:theChatGroup];
    return ;
}


/*!
 @method     同步方法
 @brief      获取该群所有成员
 @param      groupid 群组Id
 @discussion 若groupId不存在，返回nil
 @result     返回群组成员ChatGroupMember对象数组
 */
- (NSMutableArray *)getAllGroupMembersByGroupId:(NSString *)groupId
{
    if (groupId == nil) {
        return nil;
    }
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (theChatGroup == nil) {
        return nil;
    }
    NSMutableArray *groupMarray = [ChatGroupMgr readGroupMemberArrayByGroupId:groupId];
    return groupMarray;
}

/*!
 @method     同步方法
 @brief      读取某一群组的某一个成员信息
 @param      groupid 群组Id
 @param      memberId 群成员Id
 @discussion 若groupId不存在，返回nil；若memberId不存在，返回nil
 @result     返回群组成员ChatGroupMember对象数组
 */
- (ChatGroupMember *)getGroupMembersByGroupId:(NSString *)groupId withMemberId:(NSString*)memberId
{
    if (groupId == nil ||memberId == nil) {
        return nil;
    }
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (theChatGroup == nil) {
        return nil;
    }
    ChatGroupMember *groupmember = [ChatGroupMgr readGroupMemberByGroupId:groupId withMemberId:memberId];
    return groupmember;
}

/*!
 @method
 @brief     异步方法, 邀请群组新成员
 @param     ChatContact 被邀请好友对象
 @param     groupId 要加入的群组id
 @result    回调inviteGroupMemberRspState:groupId:error:
 */
- (void)inviteGroupMember:(ChatContact *)memberObj joinGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(inviteGroupMemberRspState:groupId:error:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate inviteGroupMemberRspState:Group_GroupIdNotExist groupId:groupId error:[NSString stringWithFormat:@"群组Id %@ 不存在",groupId]];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate inviteGroupMemberRspState:Group_NotLogin groupId:groupId error:@"没有登录"];
            return;
        }
        
        if (memberObj == nil || [memberObj.ContactId isEqualToString:@""]) {
            [delegateUcf.xmppGroupDelegate inviteGroupMemberRspState:Group_GroupIdNotExist groupId:groupId error:[NSString stringWithFormat:@"成员Id %@ 不存在",memberObj.ContactId]];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现inviteGroupMemberRspState:groupId:error:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSArray *memberArry = [NSArray arrayWithObject:memberObj];
    return [self inviteGroupMemberArry:memberArry joinGroupId:groupId delegate:delegate];
}

/*!
 @method
 @brief     异步方法, 邀请群组新成员
 @param     memberArry 被邀请好友ChatContact对象列表
 @param     groupid 要加入的群组id
 @result    回调inviteGroupMemberRspState:groupId:error:
 */
- (void)inviteGroupMemberArry:(NSArray *)memberArry joinGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(inviteGroupMemberRspState:groupId:error:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate inviteGroupMemberRspState:Group_GroupIdNotExist groupId:groupId error:[NSString stringWithFormat:@"群组Id %@ 不存在",groupId]];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate inviteGroupMemberRspState:Group_NotLogin groupId:groupId error:@"没有登录"];
            return;
        }
        
        if (memberArry == nil || [memberArry count] < 1) {
            [delegateUcf.xmppGroupDelegate inviteGroupMemberRspState:Group_GroupIdNotExist groupId:groupId error:[NSString stringWithFormat:@"成员对象数组不存在"]];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现inviteGroupMemberRspState:groupId:error:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    
    NSString *unitID = [NSString stringWithFormat:@"%@%@",@"inviteGroupMember.",groupId];
    [iq addAttributeWithName:@"id" stringValue:unitID];
    
    NSXMLElement *invite = [NSXMLElement elementWithName:@"invite"];
    [invite addAttributeWithName:@"group_id" stringValue:groupId];
    
    NSInteger memberArryNum = [memberArry count];
  
#warning message - 关于添加成员的部分－－－－－
    for (int i=0; i<memberArryNum; i++)
    {
        
#warning message- 需要自己创建model相关的东西
//        ContactMemModel  *itemMemModel = [memberArry objectAtIndex:i];
        
//        ChatContact *itemContact = [memberArry objectAtIndex:i];
//        NSXMLElement *user = [NSXMLElement elementWithName:@"user" stringValue:[self convertToJid:itemMemModel.memImAccountName]];
//        [invite addChild:user];
    }
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:invite];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:unitID type:@"inviteGroupMemberArry" paraMsg:theChatGroup];
    return;
}

/*!
 @method
 @brief      异步方法, 踢出群成员
 @param      memberId 踢出成员对象
 @param      groupId 群组Id
 @result     回调kickoffGroupMemberRspState:groupId:errorMsg:
 */
- (void)kickoffGroupMember:(NSString *)memberId fromGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(kickoffGroupMemberRspState:groupId:errorMsg:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate kickoffGroupMemberRspState:Group_GroupIdNotExist groupId:groupId errorMsg:[NSString stringWithFormat:@"群组Id %@ 不存在",groupId]];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate kickoffGroupMemberRspState:Group_NotLogin groupId:groupId errorMsg:@"没有登录"];
            return;
        }
        
        if (memberId == nil || [memberId isEqualToString:@""]) {
            [delegateUcf.xmppGroupDelegate kickoffGroupMemberRspState:Group_GroupIdNotExist groupId:groupId errorMsg:[NSString stringWithFormat:@"成员Id %@ 不存在",memberId]];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现kickoffGroupMemberRspState:groupId:errorMsg:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSString *jidNameValid = [self convertToJid:memberId];
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    
    NSString *unitID = [NSString stringWithFormat:@"%@%@",@"kickoffGroupMember.",groupId];
    [iq addAttributeWithName:@"id" stringValue:unitID];
    
    NSXMLElement *delete_member = [NSXMLElement elementWithName:@"delete_member"];
    [delete_member addAttributeWithName:@"group_id" stringValue:groupId];
    [delete_member addAttributeWithName:@"jid" stringValue:jidNameValid];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:delete_member];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:unitID type:@"kickoffGroupMember" paraMsg:memberId];
    return;
}

/*!
 @method
 @brief       异步方法, 请求加入群组
 @param       message  附带消息
 @result      回调requestJoinGroupIdRspState:groupId:errorMsg:
 */
- (void)requestJoinGroupId:(NSString *)groupId message:(NSString *)message delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(requestJoinGroupIdRspState:groupId:errorMsg:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate requestJoinGroupIdRspState:Group_GroupIdNotExist groupId:groupId errorMsg:[NSString stringWithFormat:@"群组Id %@ 不存在",groupId]];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate requestJoinGroupIdRspState:Group_NotLogin groupId:groupId errorMsg:@"没有登录"];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现requestJoinGroupIdRspState:groupId:errorMsg:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"requestJoinGroupId"];
    
    NSXMLElement *apply = [NSXMLElement elementWithName:@"apply"];
    [apply addAttributeWithName:@"group_id" stringValue:groupId];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:apply];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"requestJoinGroupId" type:@"requestJoinGroupId" paraMsg:groupId];
    return;
}

/*!
 @method
 @brief       异步方法,     设置群成员昵称
 @param       groupId      群组ID
 @discussion  只针对当前登录用户有效
 @result      回调方法setGroupNicknameRspState:inGroupId:
 */
- (void)setGroupNickname:(NSString *)nickName inGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(setGroupNicknameRspState:inGroupId:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate setGroupNicknameRspState:Group_GroupIdNotExist inGroupId:groupId];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate setGroupNicknameRspState:Group_NotLogin inGroupId:groupId];
            return;
        }
        
        if (nickName == nil || [nickName isEqualToString:@""]) {
            [delegateUcf.xmppGroupDelegate setGroupNicknameRspState:Group_GroupNickNameIsNull inGroupId:groupId];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现setGroupNicknameRspState:inGroupId:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    
    NSString *unitID = [NSString stringWithFormat:@"%@%@",@"setGroupNickname.",groupId];
    [iq addAttributeWithName:@"id" stringValue:unitID];
    
    NSXMLElement *nickname = [NSXMLElement elementWithName:@"nickname"];
    [nickname addAttributeWithName:@"nickname" stringValue:nickName];
    [nickname addAttributeWithName:@"group_id" stringValue:groupId];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:nickname];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:unitID type:@"setGroupNickname" paraMsg:nickName];
    return;
}

/*!
 @method
 @brief       异步方法， 主动退出某群组
 @param       groupId 要退出的群组Id
 @discussion
 @result      回调方法quitGroupIdRspState:groupId:
 */
- (void)quitGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(quitGroupIdRspState:groupId:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate quitGroupIdRspState:Group_GroupIdNotExist groupId:groupId];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate quitGroupIdRspState:Group_NotLogin groupId:groupId];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现quitGroupIdRspState:groupId:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];

    NSString *unitID = [NSString stringWithFormat:@"%@%@",@"quitGroupId.",groupId];
    [iq addAttributeWithName:@"id" stringValue:unitID];
    
    NSXMLElement *quit_group = [NSXMLElement elementWithName:@"quit_group"];
    [quit_group addAttributeWithName:@"group_id" stringValue:groupId];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:quit_group];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:unitID type:@"quitGroupId" paraMsg:groupId];
    return;
}


/*!
 @method
 @brief       异步方法， 接收加入某群的邀请 (管理员收到请求后允许申请者加入)
 @param       memberId 发出邀请的成员Id
 @param       groupId  将要加入的群组Id
 @result      回调方法acceptGroupMemberIdRspState:memberId:joinGroupId:
 */
- (void)acceptGroupMemberId:(NSString *)memberId toJoinGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(acceptGroupMemberIdRspState:memberId:joinGroupId:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate acceptGroupMemberIdRspState:Group_GroupIdNotExist memberId:memberId joinGroupId:groupId];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate acceptGroupMemberIdRspState:Group_NotLogin memberId:memberId joinGroupId:groupId];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现acceptGroupMemberIdRspState:memberId:joinGroupId:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"acceptGroupInvite"];
    
    NSXMLElement *allow_apply = [NSXMLElement elementWithName:@"allow_apply"];
    [allow_apply addAttributeWithName:@"group_id" stringValue:groupId];
    [allow_apply addAttributeWithName:@"jid" stringValue:[self convertToJid:memberId]];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:allow_apply];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"acceptGroupInvite" type:@"acceptGroupMemberId" paraMsg:nil];
    return;
}

/*!
 @method
 @brief       异步方法， 拒绝加入某群的邀请 (管理员收到请求后拒绝申请者加入)
 @param       memberId 发出邀请的成员Id
 @param       groupId  将要加入的群组Id
 @result      回调方法rejectGroupMemberIdRspState:memberId:joinGroupId:
 */
- (void)rejectGroupMemberId:(NSString *)memberId toJoinGroupId:(NSString *)groupId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(rejectGroupMemberIdRspState:memberId:joinGroupId:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate rejectGroupMemberIdRspState:Group_GroupIdNotExist memberId:memberId joinGroupId:groupId];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate rejectGroupMemberIdRspState:Group_NotLogin memberId:memberId joinGroupId:groupId];
            return;
        }
        
        if (memberId == nil || [memberId isEqualToString:@""]) {
            [delegateUcf.xmppGroupDelegate rejectGroupMemberIdRspState:Group_GroupMemberIdIsNull memberId:memberId joinGroupId:groupId];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现rejectGroupMemberIdRspState:memberId:joinGroupId:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:@"rejectGroupMemberId"];
    
    NSXMLElement *deny_apply = [NSXMLElement elementWithName:@"deny_apply"];
    [deny_apply addAttributeWithName:@"group_id" stringValue:groupId];
    [deny_apply addAttributeWithName:@"jid" stringValue:[self convertToJid:memberId]];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:deny_apply];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:@"rejectGroupMemberId" type:@"rejectGroupMemberId" paraMsg:nil];
    return;
}

/*!
 @method
 @brief      异步方法， 管理员移交管理员身份 (将自己管理员身份移交给其他人员，不需要对方确认，但对方会受到通知)
 @param      groupId  将要转换管理员权限的群组Id
 @param      adminId  将要接收管理员身份的新管理员Id
 @result     回调方法abdicateAdmininstrationGroupIdRspState:groupId:
 */
- (void)abdicateAdmininstrationGroupId:(NSString *)groupId toAdminId:(NSString *)adminId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppGroupDelegate = delegate;
    
    ChatGroup *theChatGroup = [ChatGroupMgr readChatGroupMsgbyGroupId:groupId];
    if (delegateUcf.xmppGroupDelegate && [delegateUcf.xmppGroupDelegate respondsToSelector:@selector(abdicateAdmininstrationGroupIdRspState:groupId:)])
    {
        if (theChatGroup == nil) {
            [delegateUcf.xmppGroupDelegate abdicateAdmininstrationGroupIdRspState:Group_GroupIdNotExist groupId:groupId];
            return;
        }
        
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppGroupDelegate abdicateAdmininstrationGroupIdRspState:Group_NotLogin groupId:groupId];
            return;
        }
        
        if (adminId == nil || [adminId isEqualToString:@""]) {
            [delegateUcf.xmppGroupDelegate abdicateAdmininstrationGroupIdRspState:Group_GroupAdimIdIsNull groupId:groupId];
            return;
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提示"
                                    message:@"请添加EWxChatMgrGroupDelegate协议并实现abdicateAdmininstrationGroupIdRspState:groupId:方法！"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
        return;
    }
    
    NSString *unitID = [NSString stringWithFormat:@"%@%@=%@",@"abdicateAdmininstrationGroupId.",groupId,[self convertToJid:adminId]];
    
    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:kXMPPGroupDomain];
    [iq addAttributeWithName:@"id" stringValue:unitID];
    
    NSXMLElement *set_admin = [NSXMLElement elementWithName:@"set_admin"];
    [set_admin addAttributeWithName:@"group_id" stringValue:groupId];
    [set_admin addAttributeWithName:@"jid" stringValue:[self convertToJid:adminId]];
    
    NSXMLElement *set_body = [NSXMLElement elementWithName:@"set_body"];
    [set_body addAttributeWithName:@"group_id" stringValue:groupId];
    [set_body addAttributeWithName:@"jid" stringValue:[self convertToJid:adminId]];
    [set_admin addChild:set_body];
    
    NSXMLElement *group = [NSXMLElement elementWithName:@"group" xmlns:@"http://www.ucfgroup.com/protocol/group"];
    [group addChild:set_admin];
    
    [iq addChild:group];
    
    [delegateUcf.xmppStream sendElement:iq];
    
    [delegateUcf addXmppRecordReqId:unitID type:@"abdicateAdmininstrationGroupId" paraMsg:nil];
    return;
}

//判断当前用户是不是这个群的群主 ----fusuwei 重点关注
- (BOOL)isTheOwnerOfGroupId:(NSString *)groupId
{
    //读取登录用户信息
    ChatLoginUser *login = [ChatLoginUserMgr getCurLoginUser];
    NSArray *memberArray = [ChatGroupMgr readGroupMemberArrayByGroupId:groupId];
    for (ChatGroupMember *item in memberArray) {
        if ([item.member_id isEqualToString:[Utility getBareContactId:login.UserId]]) {
            if ([item.member_affiliations isEqualToString:@"owner"]) {
                return YES;
            }
            break;
        }
    }
    return NO;
}


- (void)registDelegate:(id<EWxChatMgrGroupDelegate>)delegate
{
    WXLog(@"IWxChatMgrGroup:registDelegate");
    [UcfgroupXMPPManger sharedInstance].xmppGroupDelegate = delegate;
}

- (void)unregistDelegate:(id<EWxChatMgrGroupDelegate>)delegate
{
    WXLog(@"IWxChatMgrGroup:unregistDelegate");
    [UcfgroupXMPPManger sharedInstance].xmppGroupDelegate = nil;
}


/*!
 @method
 @brief       同步方法， 判断在GroupList中该GroupId是否存在
 @param       groupId  将要加入的群组Id
 @result      YES/NO
 */
- (BOOL)judgeInGroupListIsHaveGroupId:(NSString*)groupId
{
    if (groupId == nil || [groupId isEqualToString:@""]) {
        return NO;
    }
    return [ChatGroupMgr judgeInGroupListIsHaveGroupId:groupId];
}

/*!
 @method
 @brief       同步方法， 删除与某个联系人聊天所有记录
 @param       groupId  将要删除的群聊Id
 @result      YES/NO
 */
- (BOOL)deleteAllMessageToContactId:(NSString *)contactId
{
    if (contactId == nil || [contactId isEqualToString:@""]) {
        return NO;
    }
    return [ChatMessageMgr deleteAllMessageToContactId:contactId];
}

#pragma mark-------通用处理方法---------
- (NSString *)convertToJid:(NSString *)jidName
{
    WXLog(@"convertToJid: jidName= %@",jidName);
    NSString *jidNameValid = nil;
    if (jidName != nil)
    {
        jidNameValid = [NSString stringWithString:jidName];
        NSRange range = [jidNameValid rangeOfString:kXMPPmyDomain];
        if (range.length >0)//包含
        {
        }
        else //不包含
        {
            jidNameValid = [NSString stringWithFormat:@"%@%@",jidName,kXMPPmyDomain];
        }
    }
    else
    {
        WXLog(@"convertToJid: jidName=NULL");
    }
    return jidNameValid;
}



@end
